﻿@model Product

@functions {
    private string GetCategoryNameId(Category category)
    {
        while (category.Level != 2)
        {
            category = category.Parent;
        }

        return category.NameId;
    }
}

@{
    var imgHp = new ImageHelper();
    var strHp = new StringHelper();

    bool lazyLoad = ViewBag.LazyLoad;

    string title = String.Format("{0} {1} - {2}", Model.Category.Name, Model.Name, Model.Code);

    string url = Url.Action("Details", "Product", new
    {
        productNameId = Model.NameId,
        categoryNameId = GetCategoryNameId(Model.Category)
    });

    // Styles
    float minPrice = 0;
    float maxPrice = 0;
    if (Model.Styles.Count > 0)
    {
        minPrice = Model.Styles.Min(x => x.UnitPrice);
        maxPrice = Model.Styles.Max(x => x.UnitPrice);
    }

    // Images
    Image image = Model.Images.OrderByDescending(x => x.Priority).FirstOrDefault();
    string imageUrl = "/img/no-image.png?w=400&h=400";
    string imageLazy = "/img/lazy-product-square.png";
    string imagePadding = "padding: 17% 0;";
    if (image != null)
    {
        if (imgHp.Standard(image.Width, image.Height))
        {
            imageUrl = image.Url + "?w=400";
            imageLazy = "/img/lazy-product.png";
            imagePadding = "";
        }
        else
        {
            imageUrl = image.Url + "?w=400&h=400";
        }
    }
}

<div class="iProduct">
    <div class="image">
        @if (User.IsInRole("Admin") || User.IsInRole("Product Edit Public")
            || (User.IsInRole("Product Edit Private") && Model.CreatedBy == User.Identity.Name))
        {
            <a href="@Url.Action("Index", "Product", new { area = "Admin", search = Model.Code })"
               target="_blank"
               class="btn btn-primary btnEditProduct">
                <i class="fa fa-edit fa-2x"></i>
            </a>
        }

        <a href="@url">
            @if (lazyLoad == true)
            {
                <img src="@imageLazy"
                     class="img-thumbnail lazy-load"
                     data-original="@imageUrl"
                     title="@title"
                     alt="@title @ViewBag.AltGlobal"
                     style="@imagePadding" />
            }
            else
            {
                <img src="@imageUrl"
                     class="img-thumbnail"
                     title="@title"
                     alt="@title @ViewBag.AltGlobal"
                     style="@imagePadding" />
            }
        </a>
    </div>
    <div class="name">@Model.Name - @Model.Code</div>
    <div class="price">
        <span class="list-price">@strHp.PriceFormat(Model.ListPrice, "usd")</span>
        <span class="chomy-price">
            @if (minPrice == maxPrice)
            {
                @strHp.PriceFormat(minPrice, "usd")
            }
            else
            {
                @strHp.PriceFormat(minPrice, "usd") @:- @strHp.PriceFormat(maxPrice, "usd")
            }
        </span>
    </div>
</div>
